import QueryString from "querystring-browser";

function requestFunc(opts, callback) {
  var xhr = new XMLHttpRequest({ mozSystem: true });
  xhr.open(
    opts.method || "GET",
    opts.uri + (opts.qs ? "?" + QueryString.stringify(opts.qs) : "")
  );
  if (opts.json) xhr.setRequestHeader("Content-type", "application/json");
  xhr.onload = () =>
    callback(
      null,
      { statusCode: xhr.status, headers: xhr.getAllResponseHeaders() },
      xhr.response
    );
  xhr.onerror = () =>
    callback(
      new Error(),
      { statusCode: xhr.status, headers: xhr.getAllResponseHeaders() },
      xhr.response
    );
  xhr.send(opts.body);
}

export default requestFunc;
